#!/bin/ksh

echo "Please input the DATE of Backup!!"
echo "The Date format is: yyyymmdd"
echo "Press enter directly is to show Today's Backup logs"
read MYDATE

TEST=mytest$MYDATE

MYYEAR=`expr substr "$MYDATE" 1 4` 
MYMONTH=`expr substr "$MYDATE" 5 2` 
MYDAY=`expr substr "$MYDATE" 7 2` 

if [ $TEST = mytest ];then
  TZ=TZ+24
  Serach_date=`date +%Y`/`date +%m`/`date +%d`
  Serach_month=`date +%Y%m` 
  RmanLogdate=`date +%Y%m%d`
else 
  Serach_month=${MYYEAR}${MYMONTH}
  Serach_date=${MYYEAR}/${MYMONTH}/${MYDAY}
  RmanLogdate=$MYDATE
fi


RmanLOG=/home/oracle/scripts/rmanlog_${RmanLogdate}.log
if [ -f $RmanLOG ]
then
  cat $RmanLOG | grep -v "archive log filename" >/tmp/rmanbackup.log
  TEST1=`grep -c "Recovery Manager complete" /tmp/rmanbackup.log`
  LineEnd=`sed -n '/Finished backup/=' /tmp/rmanbackup.log`
  set -A LineNum `echo ${LineEnd}`
  CT=${#LineNum[*]}
  ((CT=CT-1))
  sed -n "1,${LineNum[$CT]}p" /tmp/rmanbackup.log >/tmp/rman.log
  tail -n5 /tmp/rmanbackup.log >>/tmp/rman.log
  if [ $TEST1 -gt 0 ]
  then
    TEST2=`grep -c "RMAN-" /tmp/rmanbackup.log`
    if [ $TEST2 -gt 0 ]
    then
      echo "                                    "
      echo "*****  Rman Maybe Have Error *******"
      echo "                                    "
      echo "*** Do You Want To See Detail ******"
      echo "*** Press Yes(y) to See Detail *****"
      read MYYES
      case  $MYYES in
        Y|y)	cat /tmp/rmanbackup.log |more
	;;
	*) exit
	;;
      esac
    else
      echo "                                         "
      echo "**** Rman Have Finish Sucessfully *******"
      echo "                                         "
      echo "****** Some Detail As Following    ******"
      echo "****** Do You Want To See Detail ********"
      echo "****** Press Yes(y) to See Detail *******"
      read MYYES
      case  $MYYES in
        Y|y)    cat /tmp/rman.log |more
        ;;
        *) exit
        ;;
      esac
    fi
  else
   echo "                                      "
   echo "******* Rman Maybe Runing Now ********"
   echo "                                      "
   echo "*****  Some Detail As Following ******"
   echo "**** Press Yes(y) to See Detail ******"
   read MYYES
   case  $MYYES in
     Y|y)    cat /tmp/rmanbackup.log |more
     ;;
     *) exit
     ;;
   esac
  fi
else
  echo "                                      "
  echo " ******* Rman Script Not Run *********"
  echo " *** Or Rman Log had deleted By SA ***"
fi
